#!/usr/bin/python

import sys

"""
setting['type']: 'variable'/'getset'
setting['name']: name
setting['ctype']: type (int/bool/wxFont/ComponentInfoType etc
setting['doc']: documentation

variable:
setting['default']: default value. Everything are strings.

getset: (These are shown to the python scripts as normal settings)
setting['setter']: C setter code (value is passed as the variable 'value') (should return true/false)
setting['getter']: C getter code (should return value)
"""

settings_def = [
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_selected", 'default':"200,200,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_hole_selection", 'default':"255,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_footprint_graphics", 'default':"0,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_jumper_wire_top", 'default':"255,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_jumper_wire_bottom", 'default':"100,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_jumper_wire_selected_head", 'default':"0,150,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_pin_through_hole", 'default':"0,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_cut", 'default':"0,180,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_automatic", 'default':"180,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_narrowcut", 'default':"0,180,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_board_pen", 'default':"150,150,150"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_board_track", 'default':"255,255,255"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_board_hole", 'default':"220,220,220"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_board_hl", 'default':"255,100,100"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_board_bg", 'default':"200,200,200"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_pin_textbg", 'default':"200,200,255"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_pin_textfg", 'default':"0,0,100"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_component_textbg", 'default':"200,255,200"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_component_textfg", 'default':"0,100,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_ratsnest", 'default':"150,150,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_ratsnest_inv", 'default':"255,0,0"},
	{'type':'variable', 'ctype':"wxColour", 'name':"colour_ratsnest_dot", 'default':"100,100,0"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_footprint_graphics", 'default':"15"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_jumper_wire_top", 'default':"35"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_jumper_wire_bottom", 'default':"50"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_jumper_wire_selected_head", 'default':"15"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_pin_through_hole", 'default':"10"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_cut", 'default':"25"},
	{'type':'variable', 'ctype':"int", 'name':"linewidth_narrowcut", 'default':"25"},
	{'type':'variable', 'ctype':"int", 'name':"radius_jumper_wire_selected_head", 'default':"50"},
	{'type':'variable', 'ctype':"double", 'name':"default_scale", 'default':"0.15"},
	{'type':'variable', 'ctype':"int", 'name':"default_board_w", 'default':"40"},
	{'type':'variable', 'ctype':"int", 'name':"default_board_h", 'default':"30"},
	{'type':'variable', 'ctype':"int", 'name':"board_hole_radius", 'default':"15"},
	{'type':'variable', 'ctype':"int", 'name':"board_min_padding_log_x", 'default':"25"},
	{'type':'variable', 'ctype':"int", 'name':"board_min_padding_log_y", 'default':"25"},
	{'type':'variable', 'ctype':"int", 'name':"drag_hysteresis", 'default':"5"},
	{'type':'variable', 'ctype':"wxFont", 'name':"font_board_text", 'default':"60, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL"},
	{'type':'variable', 'ctype':"wxFont", 'name':"font_pin_text", 'default':"30, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL"},
	{'type':'variable', 'ctype':"wxFont", 'name':"font_component_text", 'default':"30, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL"},
	{'type':'variable', 'ctype':"int", 'name':"rightbook_width", 'default':"260"},
	{'type':'variable', 'ctype':"bool", 'name':"select_touch_is_enough", 'default':"false"},
	{'type':'variable', 'ctype':"bool", 'name':"automatic_rats_nest", 'default':"true"},
	{'type':'variable', 'ctype':"bool", 'name':"automatic_cuts", 'default':"true"},
	#{'type':'variable', 'ctype':"wxString", 'name':"default_project_name", 'default':"Project"},
	{'type':'variable', 'ctype':"int", 'name':"show_pin_info", 'default':"2", 'doc':'1=name, 2=num'},
	{'type':'variable', 'ctype':"int", 'name':"width_pin_max", 'default':"300"},
	{'type':'variable', 'ctype':"ComponentInfoType", 'name':"component_show_infotype", 'default':"COMPONENT_INFO_VALUE", 'doc':"COMPONENT_INFO_NONE/COMPONENT_INFO_REFDES/COMPONENT_INFO_VALUE"},
	{'type':'getset', 'ctype':"wxString", 'name':"current_file_path",
'getter':'''if(wxGetApp().m_project == NULL) return _T("");
return wxGetApp().m_project->GetFilepath();''',
'setter':'''if(wxGetApp().m_project == NULL)
return false; wxGetApp().m_project->SetFilepath(value); return true;'''},
	{'type':'variable', 'ctype':'bool', 'name':'verbose_mode_on', 'default':'false'},
]

#out_h = sys.stdout
out_h = open('settings.h', 'w')

#out_c = sys.stdout
out_c = open('settings.cpp', 'w')

doc = open('doc/settings.txt', 'w')
doc.write(
'''----------------------------------------------------------------------------
This file contains an automatically generated listing of the settings available
to the Python API functions juku.set_setting(name, value) and
juku.get_setting(name).
----------------------------------------------------------------------------

''')

variable_declaration = ''
constructor_code = ''
constructor_init_values = ''
setsettingstring_code = ''
getsettingstring_code = ''

def add_setting(setting, is_last):
	global out_h, out_c
	global constructor_init_values, variable_declaration
	global setsettingstring_code, getsettingstring_code
	if len(setting) < 3:
		sys.stderr.write('setting_def '+type+' '+name+' has less than 3 cells\n')
		return
	
	type = setting['type']
	ctype = setting['ctype']
	name = setting['name']
	varname = 'm_'+name
	doc_text = ""
	try:
		doc_text = setting['doc']
	except KeyError:
		pass

	# Add to documentation
	doc.write(type+' '+name+'\n')
	
	if type == 'variable':
		default = setting['default']
		
		if ctype == 'wxColour':
			doc.write('\tdefault: rbg('+default+')\n')
		else:
			doc.write('\tdefault: '+default+'\n')
			
	if doc_text != "":
		doc.write('\tinfo: '+doc_text+'\n')

	if type == 'getset':
		getter = setting['getter']
		getter = getter.replace(r'\n', '\n\t\t')
		setter = setting['setter']
		setter = setter.replace(r'\n', '\n\t\t')
		gettername = 'get_'+name
		settername = 'set_'+name
		#doc.write('\t'+settername+'/'+gettername+'\n')

	
	if type == 'variable':
		# Add to header
		variable_declaration += "\t"+ctype+" "+varname+";\n"

		# Add to constructor
		if ctype == 'wxString':
			constructor_init_values += varname+'(_T(\"'+default+'\"))'
		else:
			constructor_init_values += varname+'('+default+')'

		constructor_init_values += ',\n'

		#if not is_last:
		#	constructor_init_values += ',\n'
		#else:
		#	constructor_init_values += '\n'
	#if type == 'getset':
	#	variable_declaration += '\tvoid '+settername+'('+ctype+' value);\n'
	#	variable_declaration += '\t'+ctype+' '+gettername+'();\n'

	# Add to SetSetting
	setsettingstring_code += '\tif(strcmp(name, "'+name+'") == 0){\n'
	if type == 'getset':
		setsettingstring_code += '\t\t'+setter+'\n'
	elif ctype == 'wxString':
		setsettingstring_code += '\t\t'+varname+' = value;\n'
		setsettingstring_code += '\t\treturn true;\n'
	elif ctype == 'int':
		setsettingstring_code += '\t\tlong int intvalue;\n'
		setsettingstring_code += '\t\tif(!value.ToLong(&intvalue)) return false;\n'
		setsettingstring_code += '\t\t'+varname+' = (int)intvalue;\n'
		setsettingstring_code += '\t\treturn true;\n'
	elif ctype == 'double':
		setsettingstring_code += '\t\tstd::istringstream ss(std::string(value.mb_str(wxConvUTF8)));\n'
		setsettingstring_code += '\t\tss.imbue(std::locale("C"));\n'
		setsettingstring_code += '\t\tss >> '+varname+';\n'
		setsettingstring_code += '\t\treturn true;\n'
	elif ctype == 'bool':
		setsettingstring_code += '\t\tif     (value.Lower() == _T("true" )){ '+varname+' = true;  }\n'
		setsettingstring_code += '\t\telse if(value.Lower() == _T("false")){ '+varname+' = false; }\n'
		setsettingstring_code += '\t\telse return false;\n'
		setsettingstring_code += '\t\treturn true;\n'
	elif ctype == 'wxColour':
		setsettingstring_code += '\t\tbool b = '+varname+'.Set(value);\n'
		setsettingstring_code += '\t\treturn b;\n'
	elif ctype == 'ComponentInfoType':
		setsettingstring_code += '\t\tif(value == _T("COMPONENT_INFO_NONE")) '+varname+' = COMPONENT_INFO_NONE;\n'
		setsettingstring_code += '\t\telse if(value == _T("COMPONENT_INFO_REFDES")) '+varname+' = COMPONENT_INFO_REFDES;\n'
		setsettingstring_code += '\t\telse if(value == _T("COMPONENT_INFO_VALUE")) '+varname+' = COMPONENT_INFO_VALUE;\n'
		setsettingstring_code += '\t\telse return false;\n'
		setsettingstring_code += '\t\treturn true;\n'
	else:
		setsettingstring_code += '\t\treturn false;\n'
		doc.write('\tinfo: Cannot be changed through Python API or SetSetting().\n')
	setsettingstring_code += '\t}\n'

	# Add to GetSetting
	getsettingstring_code += '\tif(strcmp(name, "'+name+'") == 0){\n'
	if type == 'getset':
		getsettingstring_code += '\t\t'+getter+'\n'
	elif ctype == 'wxString':
		getsettingstring_code += '\t\treturn '+varname+';\n'
	elif ctype == 'int':
		getsettingstring_code += '\t\treturn wxString::Format(_T("%i"), '+varname+');\n'
	elif ctype == 'double':
		getsettingstring_code += '\t\tstd::ostringstream ss;\n'
		getsettingstring_code += '\t\tss.imbue(std::locale("C"));\n'
		getsettingstring_code += '\t\tss << '+varname+';\n'
		getsettingstring_code += '\t\treturn wxString(ss.str().c_str(), wxConvUTF8);\n'
	elif ctype == 'bool':
		getsettingstring_code += '\t\treturn '+varname+' ? _T("True") : _T("False");\n'
	elif ctype == 'wxColour':
		getsettingstring_code += '\t\treturn '+varname+'.GetAsString(wxC2S_CSS_SYNTAX);\n'
	elif ctype == 'ComponentInfoType':
		getsettingstring_code += '\t\tswitch('+varname+'){\n'
		getsettingstring_code += '\t\t\tcase COMPONENT_INFO_NONE: return _T("COMPONENT_INFO_NONE");\n'
		getsettingstring_code += '\t\t\tcase COMPONENT_INFO_REFDES: return _T("COMPONENT_INFO_REFDES");\n'
		getsettingstring_code += '\t\t\tcase COMPONENT_INFO_VALUE: return _T("COMPONENT_INFO_VALUE");\n'
		getsettingstring_code += '\t\t}\n'
	else:
		doc.write('\tinfo: Cannot be fetched through Python API or GetSetting().\n')
	getsettingstring_code += '\t}\n'

i=0
for setting in settings_def:
	i+=1
	add_setting(setting, i == len(settings_def))

doc.write('\n')

constructor_init_values = constructor_init_values[0:len(constructor_init_values)-2] + '\n'

setsettingstring_code += '\treturn false;\n'
getsettingstring_code += '\treturn _T("");\n'

out_h.write(
'''
/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
	This file is generated by generate_settings.py at compile-time.
*/

#ifndef SETTINGS_H_
#define SETTINGS_H_

#include "common.h"
#include "wx/wx.h"
#include "component.h"

class Settings
{
public:
	Settings();
	//returns false if setting name is invalid or the value is invalid
	bool SetSettingString(const char *name, wxString value);
	wxString GetSettingString(const char *name);

'''
+
variable_declaration
+
'''
};
#endif
'''
)

out_c.write(
'''/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
	This file is generated by generate_settings.py at compile-time.
*/

#include "common.h"
#include "settings.h"
#include "jukuapp.h"

Settings::Settings()
:
'''
+constructor_init_values
+'{\n'
+constructor_code
+'}\n'
+'''bool Settings::SetSettingString(const char *name, wxString value)
{
'''
+setsettingstring_code
+'}\n'
+'''wxString Settings::GetSettingString(const char *name)
{
'''
+getsettingstring_code
+'}\n'
)

out_h.close()
out_c.close()
doc.close()


